#include "StartLayer.h"
#include <iostream>
#include "ConstClass.h"
#include "PlayLayer.h"
#include "AudioManage.h"
#include "ExitLayer.h"

#define PNG_START_BACKGROUND "Start/start_back.png"
#define PNG_SYMBOLE1_TURTLE_RED "Start/symbol_red.png"
#define PNG_SYMBOLE1_TURTLE_YELLOW "Start/symbol_yellow.png"
#define PNG_SYMBOLE1_TURTLE_BLUE "Start/symbol_blue.png"
#define PNG_SYMBOLE1_TURTLE_GREEN "Start/symbol_green.png"
#define PNG_SYMBOLE1_TURTLE_PURPLE "Start/symbol_purple.png"
#define PNG_SYMBOLE2_TURTLE_RED "Start/symbol2_red.png"
#define PNG_SYMBOLE2_TURTLE_YELLOW "Start/symbol2_yellow.png"
#define PNG_SYMBOLE2_TURTLE_BLUE "Start/symbol2_blue.png"
#define PNG_SYMBOLE2_TURTLE_GREEN "Start/symbol2_green.png"
#define PNG_SYMBOLE2_TURTLE_PURPLE "Start/symbol2_purple.png"
#define PNG_BTN_START_N "Start/btn_start_n.png"
#define PNG_BTN_START_S "Start/btn_start_s.png"
#define PNG_BTN_HELP_N "Start/btn_help_n.png"
#define PNG_BTN_HELP_S "Start/btn_help_s.png"
#define PNG_BTN_EXIT_N "Start/btn_exit_n.png"
#define PNG_BTN_EXIT_S "Start/btn_exit_s.png"
#define PNG_BTN_COMGAME_N "Start/btn_comgame_n.png"
#define PNG_BTN_COMGAME_S "Start/btn_comgame_s.png"
#define PNG_BTN_NETGAME_N "Start/btn_netgame_n.png"
#define PNG_BTN_NETGAME_S "Start/btn_netgame_s.png"
#define PNG_BTN_3PLAYER_N "Start/btn_playercount3_n.png"
#define PNG_BTN_3PLAYER_S "Start/btn_playercount3_s.png"
#define PNG_BTN_4PLAYER_N "Start/btn_playercount4_n.png"
#define PNG_BTN_4PLAYER_S "Start/btn_playercount4_s.png"
#define PNG_BTN_5PLAYER_N "Start/btn_playercount5_n.png"
#define PNG_BTN_5PLAYER_S "Start/btn_playercount5_s.png"
#define PNG_BTN_BACK_N "Start/btn_back_n.png"
#define PNG_BTN_BACK_S "Start/btn_back_s.png"

#define TIME_STOP 2.0f
#define TIME_MOVE 2.0f
#define TIME_FADE_OUT  0.3f
#define TIME_FADE_IN  0.3f

int btnLayer = 0;

StartLayer::StartLayer()
{
}


StartLayer::~StartLayer()
{
}

bool StartLayer::init()
{
	ConstClass::MY_NAME = UserDefault::sharedUserDefault()->getStringForKey("MyName", "");

	Size size = Director::getInstance()->getVisibleSize();

	Sprite* bgSprite = Sprite::create(PNG_START_BACKGROUND);
	bgSprite->setPosition(Vec2(size.width / 2, size.height / 2));

	this->addChild(bgSprite, 0);

	std::string symbol1s[5] = {PNG_SYMBOLE1_TURTLE_RED,
	PNG_SYMBOLE1_TURTLE_YELLOW,
	PNG_SYMBOLE1_TURTLE_BLUE,
	PNG_SYMBOLE1_TURTLE_GREEN,
	PNG_SYMBOLE1_TURTLE_PURPLE};

	std::string symbol2s[5] = {PNG_SYMBOLE2_TURTLE_BLUE,
	PNG_SYMBOLE2_TURTLE_GREEN,
	PNG_SYMBOLE2_TURTLE_PURPLE,
	PNG_SYMBOLE2_TURTLE_RED,
	PNG_SYMBOLE2_TURTLE_YELLOW};

	for (int i = 0; i < 5; i++)
	{
		Sprite* t1 = Sprite::create(symbol1s[i]);
		t1->setPosition(Vec2(size.width * -0.5f,size.height * 0.43f));
		this->addChild(t1);

		Sprite* t2 = Sprite::create(symbol2s[i]);
		t2->setPosition(Vec2(size.width * -0.9f, size.height * 0.43f));
		this->addChild(t2);

		this->turtles[i * 2] = t1;
		this->turtles[i * 2 + 1] = t2;
	}

	turtles[0]->setPositionX(size.width * 0.7f);
	turtles[1]->setPositionX(size.width * 0.3f);

	this->startItem = MenuItemImage::create(PNG_BTN_START_N, PNG_BTN_START_S, CC_CALLBACK_0(StartLayer::startCallback, this));
	startItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.30f));
	startItem->setScale(0.8f);
	
	this->helpItem = MenuItemImage::create(PNG_BTN_HELP_N, PNG_BTN_HELP_S, CC_CALLBACK_0(StartLayer::helpCallback, this));
	helpItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.20f));
	helpItem->setScale(0.8f);

	this->exitItem = MenuItemImage::create(PNG_BTN_EXIT_N, PNG_BTN_EXIT_S, CC_CALLBACK_0(StartLayer::exitCallback, this));
	exitItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.10f));
	exitItem->setScale(0.8f);

	this->comGameItem = MenuItemImage::create(PNG_BTN_COMGAME_N, PNG_BTN_COMGAME_S, CC_CALLBACK_0(StartLayer::comGameCallback, this));
	comGameItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.30f));
	comGameItem->setScale(0.8f);
	comGameItem->setVisible(false);

	this->netGameItem = MenuItemImage::create(PNG_BTN_NETGAME_N, PNG_BTN_NETGAME_S, CC_CALLBACK_0(StartLayer::netGameCallback, this));
	netGameItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.20f));
	netGameItem->setScale(0.8f);
	netGameItem->setVisible(false);

	this->backItem = MenuItemImage::create(PNG_BTN_BACK_N, PNG_BTN_BACK_S, CC_CALLBACK_0(StartLayer::backCallback, this));
	backItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.10f));
	backItem->setScale(0.8f);
	backItem->setVisible(false);

	this->_3PlayerItem = MenuItemImage::create(PNG_BTN_3PLAYER_N, PNG_BTN_3PLAYER_S, CC_CALLBACK_0(StartLayer::playGameCallback, this, 3));
	_3PlayerItem->setPosition(Vec2(size.width * 0.18f, size.height * 0.25f));
	_3PlayerItem->setScale(0.7f);
	_3PlayerItem->setVisible(false);

	this->_4PlayerItem = MenuItemImage::create(PNG_BTN_4PLAYER_N, PNG_BTN_4PLAYER_S, CC_CALLBACK_0(StartLayer::playGameCallback, this, 4));
	_4PlayerItem->setPosition(Vec2(size.width * 0.5f, size.height * 0.25f));
	_4PlayerItem->setScale(0.7f);
	_4PlayerItem->setVisible(false);

	this->_5PlayerItem = MenuItemImage::create(PNG_BTN_5PLAYER_N, PNG_BTN_5PLAYER_S, CC_CALLBACK_0(StartLayer::playGameCallback, this, 5));
	_5PlayerItem->setPosition(Vec2(size.width * 0.82f, size.height * 0.25f));
	_5PlayerItem->setScale(0.7f);
	_5PlayerItem->setVisible(false);

	Menu* startMenu = Menu::create(startItem, helpItem, exitItem,
								   comGameItem, netGameItem, backItem,
								   _3PlayerItem, _4PlayerItem, _5PlayerItem,
									NULL);
	startMenu->setPosition(Vec2::ZERO);
	this->addChild(startMenu);

	return true;
}

Scene* StartLayer::createScene()
{
	Scene* startScene = Scene::create();
	Layer* startLayer = StartLayer::create();

	startScene->addChild(startLayer);
	return startScene;
} 

void StartLayer::onEnter()
{
	Layer::onEnter();

	float yPosition = this->turtles[0]->getPositionY();
	Size size = Director::getInstance()->getVisibleSize();

	this->turtles[0]->runAction(RepeatForever::create(
		Sequence::create(
		DelayTime::create(TIME_STOP),
		MoveTo::create( TIME_MOVE,Vec2(size.width * 1.5f,this->turtles[0]->getPositionY())),
		Place::create(Vec2(size.width * -0.5f, this->turtles[0]->getPositionY())),
		DelayTime::create(TIME_STOP * 7),
		MoveTo::create(TIME_MOVE, Vec2(size.width * 0.7f, this->turtles[0]->getPositionY())),
		NULL)
		));

	this->turtles[1]->runAction(RepeatForever::create(
		Sequence::create(
		DelayTime::create(TIME_STOP),
		MoveTo::create(TIME_MOVE, Vec2(size.width * 1.1f, this->turtles[1]->getPositionY())),
		Place::create(Vec2(size.width * -0.9f, this->turtles[1]->getPositionY())),
		DelayTime::create(TIME_STOP * 7),
		MoveTo::create(TIME_MOVE, Vec2(size.width * 0.3f, this->turtles[1]->getPositionY())),
		NULL)
		));

	for (int i = 1; i < 5; i++)
	{
		this->turtles[i * 2]->runAction(RepeatForever::create(
			Sequence::create(
			DelayTime::create(TIME_STOP * (i * 2 - 1)),
			MoveTo::create(TIME_MOVE, Vec2(size.width * 0.7f, yPosition)),
			DelayTime::create(TIME_STOP),
			MoveTo::create(TIME_MOVE, Vec2(size.width * 1.5f, yPosition)),
			Place::create(Vec2(size.width * -0.5f, yPosition)),
			DelayTime::create(TIME_STOP * (8 - i * 2)),
			NULL)
			));

		this->turtles[i * 2 + 1]->runAction(RepeatForever::create(
			Sequence::create(
			DelayTime::create(TIME_STOP * (i * 2 - 1)),
			MoveTo::create(TIME_MOVE, Vec2(size.width * 0.3f, yPosition)),
			DelayTime::create(TIME_STOP),
			MoveTo::create(TIME_MOVE, Vec2(size.width * 1.1f, yPosition)),
			Place::create(Vec2(size.width * -0.9f, yPosition)),
			DelayTime::create(TIME_STOP * (8 - i * 2)),
			NULL)
			));
	}
	
	AudioManage::playStartMusic();

}


void StartLayer::startCallback()
{
	AudioManage::buttonSound();

	btnLayer = 1;

	Node* Btns[3] = { startItem, helpItem, exitItem };
	Node* BtnsShow[3] = { comGameItem, netGameItem, backItem };

	for (int i = 0; i < 3; i++)
	{
		Btns[i]->runAction(Sequence::create(
			FadeOut::create(TIME_FADE_OUT),
			Hide::create(),
			NULL
			));
	}

	for (int i = 0; i < 3; i++)
	{
		BtnsShow[i]->runAction(Sequence::create(
			DelayTime::create(TIME_FADE_OUT),
			FadeOut::create(0),
			Show::create(),
			FadeIn::create(TIME_FADE_IN),
			NULL
			));
	}
}

void StartLayer::helpCallback()
{
	AudioManage::buttonSound();

	if (_helpLayer == nullptr)
	{
		_helpLayer = HelpLayer::create();
		this->addChild(_helpLayer, 10);
	}
	else
	{
		HelpLayer* helpLayer = (HelpLayer*)_helpLayer;
		helpLayer->backToFirstPage();
		_helpLayer->setVisible(true);
	}
}

void StartLayer::exitCallback()
{
	Director::getInstance()->end();
    
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void StartLayer::comGameCallback()
{
	AudioManage::button1Sound();

	btnLayer = 2;
	ConstClass::COM_GAME = true;

	Node* HideBtns[2] = { comGameItem, netGameItem };
	Node* ShowBtns[3] = { _3PlayerItem, _4PlayerItem, _5PlayerItem };

	for (int i = 0; i < 2; i++)
	{
		HideBtns[i]->runAction(Sequence::create(
			FadeOut::create(TIME_FADE_OUT),
			Hide::create(),
			NULL
			));
	}

	for (int i = 0; i < 3; i++)
	{
		ShowBtns[i]->runAction(Sequence::create(
			DelayTime::create(TIME_FADE_OUT),
			FadeOut::create(0),
			Show::create(),
			FadeIn::create(TIME_FADE_IN),
			NULL
			));
	}
}

void StartLayer::netGameCallback()
{
	AudioManage::button1Sound();

	btnLayer = 2;
	ConstClass::COM_GAME = false;
}

void StartLayer::backCallback()
{
	AudioManage::button2Sound();

	if (2 == btnLayer)
	{
		Node* HideBtns[3] = { _3PlayerItem, _4PlayerItem, _5PlayerItem };
		Node* ShowBtns[2] = { comGameItem, netGameItem };		

		for (int i = 0; i < sizeof(HideBtns) / sizeof(HideBtns[0]); i++)
		{
			HideBtns[i]->runAction(Sequence::create(
				FadeOut::create(TIME_FADE_OUT),
				Hide::create(),
				NULL
				));
		}

		for (int i = 0; i < sizeof(ShowBtns) / sizeof(ShowBtns[0]); i++)
		{
			ShowBtns[i]->runAction(Sequence::create(
				DelayTime::create(TIME_FADE_OUT),
				FadeOut::create(0),
				Show::create(),
				FadeIn::create(TIME_FADE_IN),
				NULL
				));
		}
	}
	else
	{
		Node* HideBtns[3] = { comGameItem, netGameItem, backItem };
		Node* ShowBtns[3] = { startItem, helpItem, exitItem };

		for (int i = 0; i < sizeof(HideBtns) / sizeof(HideBtns[0]); i++)
		{
			HideBtns[i]->runAction(Sequence::create(
				FadeOut::create(TIME_FADE_OUT),
				Hide::create(),
				NULL
				));
		}

		for (int i = 0; i < sizeof(ShowBtns) / sizeof(ShowBtns[0]); i++)
		{
			ShowBtns[i]->runAction(Sequence::create(
				DelayTime::create(TIME_FADE_OUT),
				FadeOut::create(0),
				Show::create(),
				FadeIn::create(TIME_FADE_IN),
				NULL
				));
		}
	}

	btnLayer -= 1;
}

void StartLayer::playGameCallback(int playerCount)
{
	if ("" == ConstClass::MY_NAME)
	{
		AudioManage::buttonSound();

		showNameLayer();
		return;
	}

	AudioManage::buttonSound();
	AudioManage::stopStartMusic();

	ConstClass::PLAYER_COUNT = playerCount;

	auto director = Director::getInstance();
	Scene* playScene = PlayLayer::createScene();
	//auto transitions = TransitionMoveInT::create(0.8f, playScene);

	director->replaceScene(playScene);
}

void StartLayer::showNameLayer()
{
	if (_nameLayer == nullptr)
	{
		_nameLayer = NameLayer::create();
		this->addChild(_nameLayer, 10);
	}
	else
	{
		NameLayer* nameLayer = (NameLayer*)_nameLayer;
		nameLayer->changeName();
		_nameLayer->setVisible(true);
	}
}
